//
//  WeatherSelectorView.swift
//  MyDiary2025
//
//  Created by 罗万能 on 2025/6/26.
//


import SwiftUI

struct WeatherSelectorView: View {
    @State private var selectedWeather: WeatherCondition = .sunny
    @State private var showRecommendation = false
    
    var body: some View {
        NavigationView {
            VStack(spacing: 30) {
                // 顶部标题
                Text("天气与心情")
                    .font(.largeTitle)
                    .fontWeight(.bold)
                    .foregroundColor(.primary)
                
                // 当前选中的天气详情
                ZStack {
                    RoundedRectangle(cornerRadius: 25)
                        .fill(selectedWeather.color.opacity(0.2))
                        .frame(height: 250)
                    
                    VStack(spacing: 20) {
                        selectedWeather.image
                            .font(.system(size: 100))
                        
                        Text(selectedWeather.chineseDescription)
                            .font(.title)
                            .fontWeight(.bold)
                        
                        if showRecommendation {
                            Text(selectedWeather.recommendation)
                                .font(.subheadline)
                                .foregroundColor(.secondary)
                                .multilineTextAlignment(.center)
                                .padding(.horizontal)
                        }
                    }
                }
                .padding(.horizontal)
                
                // 天气选择器
                ScrollView(.horizontal, showsIndicators: false) {
                    HStack(spacing: 15) {
                        ForEach(WeatherCondition.allCases) { weather in
                            WeatherCard(
                                weather: weather,
                                isSelected: weather == selectedWeather
                            )
                            .onTapGesture {
                                withAnimation {
                                    selectedWeather = weather
                                    showRecommendation = true
                                }
                            }
                        }
                    }
                    .padding(.horizontal)
                }
                
                Spacer()
            }
            .padding(.top, 40)
            .padding(.bottom)
            .navigationBarTitle("")
            .navigationBarHidden(true)
        }
    }
}

// 天气卡片组件
struct WeatherCard: View {
    let weather: WeatherCondition
    let isSelected: Bool
    
    var body: some View {
        VStack(spacing: 10) {
            weather.image
                .font(.system(size: 40))
            
            Text(weather.chineseDescription)
                .font(.caption)
                .fontWeight(.medium)
        }
        .frame(width: 80, height: 100)
        .background(
            RoundedRectangle(cornerRadius: 15)
                .fill(isSelected ? weather.color.opacity(0.3) : Color.gray.opacity(0.1))
                .overlay(
                    RoundedRectangle(cornerRadius: 15)
                        .stroke(isSelected ? weather.color : Color.clear, lineWidth: 2)
                )
        )
        .cornerRadius(15)
    }
}

// 预览
struct WeatherSelectorView_Previews: PreviewProvider {
    static var previews: some View {
        WeatherSelectorView()
    }
}
